package top.lilele.adminSystem.service;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.stereotype.Service;
import top.lilele.adminSystem.dto.CommonPageDto;
import top.lilele.adminSystem.dto.PageDto;
import top.lilele.adminSystem.entity.AdminNoticeEntity;
import top.lilele.adminSystem.mapper.AdminNoticeMapper;
import top.lilele.adminSystem.utils.AsyncOperate;
import top.lilele.adminSystem.utils.Utils;
import top.lilele.adminSystem.vo.NoticeInfoVo;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author lilele
 */
@Service
public class AdminNoticeService {

    @Resource
    AdminNoticeMapper adminNoticeMapper;

    @Resource
    AsyncOperate asyncOperate;

    /**
     * 新增公告
     *
     * @param adminNoticeEntity
     */
    public void add(AdminNoticeEntity adminNoticeEntity) {
        adminNoticeEntity.setCreatedId(StpUtil.getLoginIdAsString());
        // 录入公告内容
        adminNoticeMapper.insert(adminNoticeEntity);
        asyncOperate.sendWebsocketMessageAsync(adminNoticeEntity);
    }

    /**
     * 查询公告列表
     *
     * @param commonPageDto
     * @return
     */
    public PageDto queryNoticeList(CommonPageDto commonPageDto) {
        List list = adminNoticeMapper.queryNoticeList();
        PageDto pageDto = Utils.generatePage(list, commonPageDto.getPageNum(), commonPageDto.getPageSize());
        return pageDto;
    }

    /**
     * 根据id批量删除公告信息
     *
     * @param ids
     */
    public void deleteNoticeByIds(List<String> ids) {
        adminNoticeMapper.deleteBatchIds(ids);
    }

    /**
     * 根据id查询详情
     *
     * @param id
     * @return
     */
    public AdminNoticeEntity queryNoticeById(String id) {
        AdminNoticeEntity adminNoticeEntity = adminNoticeMapper.selectById(id);
        return adminNoticeEntity;
    }
}
